package com.chenglu.android.mine.beans.mvp.presenter;





import android.accounts.NetworkErrorException;

import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.base.BaseView;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.AmountTypeBean;
import com.chenglu.android.mine.bean.BeansDetailBean;
import com.chenglu.android.mine.bean.PutFileBean;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.beans.mvp.contract.BeansContract;
import com.chenglu.android.mine.beans.mvp.model.BeansModel;

import java.io.File;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/7/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class BeansPresenter extends BasePresenter<BeansContract.View> implements BeansContract.BeansPresenter {

     private  BeansContract.BeansModel model;

    public BeansPresenter(){
        model = new BeansModel();
    }

    @Override
    public void getActiveBeans(String token) {

        if(!isViewAttached()){
            return;
        }

        model.getActiveBeans(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<SmsCodeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull SmsCodeBean bean) {
                        //每次进来都会刷新token
                        if(bean.getCode().equals(Constant.HTTPCODE200)){
                            mView.getActiveBeans(bean);
                        }else {
                            mView.getActiveBeans(bean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
//                        apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getBeansDetails(String token) {
        if(!isViewAttached()){
            return;
        }

        model.getBeansDetails(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<BeansDetailBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull BeansDetailBean bean) {
                        //每次进来都会刷新token
                        if(bean.getCode().equals(Constant.HTTPCODE200)){
                            mView.getBeansDetails(bean);
                        }else {
                            mView.getBeansDetails(bean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
//                        apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    private void apiError(@NonNull Throwable e,BaseView baseView) {

        if (e instanceof ConnectException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
            ToastUtils.show("请检查您的网络状态！");
        } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
            ToastUtils.show("请求超时！");
        } else {
            //401 登录异常
            baseView.onError(e.getMessage());
        }

        LogUtils.e("qqqqqqq:" + "onError:" + e.getMessage());
    }


    @Override
    public void getAmountTypeList(String token, Map<String,String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getAmountTypeList(token, map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AmountTypeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull AmountTypeBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getAmountTypeList(bean);
                        }else {
                            mView.getAmountTypeList(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getUserInfo(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getUserInfo(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<UserInfoBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull UserInfoBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getUserInfo(bean);
                        }else {
                            mView.getUserInfo(bean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }


}
